package intelligentcity.gui;

import intelligentcity.model.City;
import intelligentcity.model.Point;
import intelligentcity.model.intersection.Intersection;
import intelligentcity.model.streets.OneWayStreet;
import intelligentcity.model.streets.Street;

import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;

public class CityDrawer {

    public CityDrawer() {

    }

    public static void drawCity(Graphics2D g2d, City city) {
        for (Street street : city.getStreets()) {
            drawStreet(g2d, street);
        }
        for (Intersection intersection : city.getIntersections()) {
            drawIntersection(g2d, intersection);
        }
        for (Point startingPoint : city.getStartPoints()) {
            drawPoint(g2d, startingPoint);
        }

    }

    public static void drawOneWayStreet(Graphics2D g2d, OneWayStreet street) {
        g2d.drawLine(street.getP1().getX(), street.getP1().getY(), street.getP2().getX(), street.getP2().getY());
    }

    public static void drawStreet(Graphics2D g2d, Street street) {
        drawOneWayStreet(g2d, street.getFirstStreet());
        drawOneWayStreet(g2d, street.getSecondStreet());
    }

    public static void drawIntersection(Graphics2D g2d, Intersection intersection) {
        drawStreet(g2d, intersection.getVerticalStreet());
        drawStreet(g2d, intersection.getHorizontalStreet());
        drawIntersectionRectangle(g2d, intersection);
    }

    public static void drawIntersectionRectangle(Graphics2D g2d, Intersection intersection) {
        int x = intersection.getHorizontalStreet().getFirstStreet().getP1().getX();
        int y = intersection.getVerticalStreet().getFirstStreet().getP1().getY();
        int width = intersection.getHorizontalStreet().getFirstStreet().getP2().getX() - x;
        int height = intersection.getVerticalStreet().getFirstStreet().getP2().getY() - y;
        g2d.drawRect(x, y, width, height);
    }

    public static void drawPoint(Graphics2D g2d, Point point) {
        g2d.fill(new Ellipse2D.Double(point.getX(), point.getY(), 10, 10));
    }

}
